import java.io.*;
import java.util.*;
import mike.util.csv.*;


/*
 * TODO Writer
 * TODO In-field quotation handling (single quotes in middle of escaped and unescaped fields)
 */

public class Test
{
	public static void main(String[] args) throws IOException
	{
		String[] headers = {"id", "name", "location" };
//		String[] line1 = {"1", "Mike", "Minneapolis" };
		String[] line1 = {"1", "Mike", "Minneapolis", "abcd" };
		String[] line2 = {"2", "John", "New York, New York" };
		String[] line3 = {"3", "Jane\nSmith" };
		
		List<String[]> rows = Arrays.asList(line1, line2, line3);
		
		CSVWriter writer = new CSVWriter("out.csv", headers, true);
//		writer.writeMultipleRows(rows);
//		writer.writeRow(line1);
//		writer.writeRow(line2);
		
		writer.writeField("1");
		System.out.println(writer.writeMultipleFields(line3));
//		System.out.println(writer.writeOneField("abcdefg"));
		writer.endRow();
		
		writer.close();
		
//		Pattern pattern = Pattern.compile("[,\"\n]");
//		//								  "[,\"\n]"
//		Matcher matcher = pattern.matcher(s);
//		while (matcher.find()) {
//			System.out.println("Found at index " + matcher.start());
//        }
		
//		CSVReader reader = new CSVReader("basic.csv", 1);
//		while (reader.hasNext())
//		{
//			String[] arr = reader.getNextRow();
//			System.out.println(reader.getLastRowNumber());
//			for (String s : arr)
//				System.out.print(makeLength('\'' + s + "' ", 12));
////			System.out.print(makeLength('\'' + reader.getValue("id") + "' ", 12));
////			System.out.print(makeLength('\'' + reader.getValue("name") + "' ", 12));
////			System.out.print(makeLength('\'' + reader.getValue("location") + "' ", 12));
//			System.out.println("\n*");
//		}
//		reader.close();
	}
//	public static String encode1(String s)
//	{
////		Matcher matcher = pattern.matcher(s);
////		if (matcher.find())
////		{
////			s = s.replace("\"", "\"\"");
////			s = '"' + s + '"';
////		}
////		return s;
//	}
	public static String encode2(String s)
	{
		if (s.contains(",") || s.contains ("\"") || s.contains("\n"))
		{
			s = s.replace("\"", "\"\"");
			s = "\"" + s + "\"";
		}
		return s;
	}
	public static String encode3(String s)
	{
		if (s.indexOf(',') >= 0 || s.indexOf(',') >= 0 || s.indexOf('\n') >= 0)
		{
			s = s.replace("\"", "\"\"");
			s = '\"' + s + '\"';
		}
		return s;
	}
	
	static String makeLength(String s, int i)
	{
		while (s.length() < i)
			s += ' ';
		return s;
	}
}
